package com.wyhgs.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyhgs.reggie.common.CustomException;
import com.wyhgs.reggie.dto.DishDto;
import com.wyhgs.reggie.entity.Dish;
import com.wyhgs.reggie.entity.DishFlavor;
import com.wyhgs.reggie.mapper.DishMapper;
import com.wyhgs.reggie.service.DishFlavorService;
import com.wyhgs.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 宛远鸿
 * @version 1.0
 */
@Slf4j
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    DishFlavorService dishFlavorService;
    /**
     * 新增菜品并同时保存口味数据
     * @param dishDto
     */
    @Transactional
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品基本信息到dish菜品表
        this.save(dishDto);

        Long dishId = dishDto.getId();
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors= flavors.stream().map((item)->{
           item.setDishId(dishId);
           return item;
        }).collect(Collectors.toList());

        //将菜品口味数据保存到菜品口味dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询菜品信息和对应口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息从dish表查询
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    /**
     *更新dish表基本信息
     * @param dishDto
     */
    @Transactional
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
    this.updateById(dishDto);
//清理当前菜品对应口味数据---dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors=flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id删除菜品以及批量
     * @param ids
     */
    @Transactional
    @Override
    public void delete(List<Long> ids) {
//select count(*) from dish where id in (...) and status=1;
        //查询菜品状态，确定是否可用删除
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(Dish::getId,ids);
        dishLambdaQueryWrapper.eq(Dish::getStatus,1);
        long count = this.count(dishLambdaQueryWrapper);
        //如果不能删除，抛出一个业务异常
        if (count >0) {
            throw new CustomException("菜品正在售卖中，不能删除");
        }
        //如果可以删除，先删除菜品表中的数据---dish
        this.removeByIds(ids);

        //delete from dish_flavor where dish_id in ids;
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.in(DishFlavor::getDishId,ids);
        //删除关系表中的数据----dish_flavor
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
        }

    /**
     * 改变售卖状态
     * @param ids
     */
    //update dish set status=#{w} where id in #{ids}
    @Override
    public void state(List<Long> ids,int w) {
        //先查询到对应ids的菜品信息list
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(Dish::getId,ids);
        List<Dish> list = this.list(dishLambdaQueryWrapper);
        //判断当前所选菜品售卖状态是否一致
        dishLambdaQueryWrapper.eq(Dish::getStatus,w);
        long count = this.count(dishLambdaQueryWrapper);
        if (count > 0) {
            throw new CustomException("当前所选菜品中的售卖状态有一个或多个售卖状态与操作后状态一致,操作失败请检查");
        }
        //改变每一个dish的status
       list= list.stream().map((item)->{
            item.setStatus(w);
            return item;
        }).collect(Collectors.toList());
        //更新updateById dish表
        this.updateBatchById(list);
    }


}
